package zhangqi.com.visiter_02;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void insert(View view) {
        //通过内容提供者把数据插入people数据库
        //拿到contentResolver---内容解析者
        ContentResolver cr = this.getContentResolver();
        ContentValues values =new ContentValues();
        values.put("name","二娃");
        values.put("money","20000");
        //url:内容提供者主机名
        Uri uri = Uri.parse("content://com.zhangqi.people/person");
        Log.i("zhangqi","内容接受者收到的uri是。。。。。。"+uri.toString());
        cr.insert(uri, values);
        //Log.i("zhangqi", "内容接受者收到的URI指向是。。。。。。" + insert);

    }
    public void select(View view) {
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(Uri.parse("content://com.zhangqi.people/person/4"),
                null,null,null,null);
        while (cursor.moveToNext()){
            String name = cursor.getString(1);
            String money = cursor.getString(2);
            Log.i("zhangqi",name+"---------"+money);
        }
    }
    public void update(View view) {
        ContentResolver cr = getContentResolver();
        ContentValues values  = new ContentValues();
        values.put("name","一娃");
        cr.update(Uri.parse("content://com.zhangqi.people/person"),values,"name=?",new String[]{"一娃"});
    }
    public void delete(View view) {
        ContentResolver cr = getContentResolver();
        cr.delete(Uri.parse("content://com.zhangqi.people/person"),"name=?",new String[]{"一娃"});
    }
}
